<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller {

	//用户登录
	public function index() {
		if (IS_POST) {
			$msg = D('User')->UserLogin();
			if ($msg['status']) {

				session('auth', $msg['info']);
				//处理来路
				$u = I('get.u');
				if ($u) {
					$msg['url'] = str_replace('.json', '', U(str_replace('_', '/', $u)));
				}
				$this->success($msg['msg'], $msg['url']);
			} else {
				$this->error($msg['msg']);
			}

		} else {
			$this->display();
		}

	}
	//显示验证码
	Public function verify() {
		// 用法 buildImageVerify($length,$mode,$type,$width,$height,$verifyName)
		// 用法 length 验证码的长度，默认为4位数
		// type 验证码的图片类型，默认为png
		// width 验证码的宽度，默认会自动根据验证码长度自动计算
		// height 验证码的高度，默认为22
		// verifyName 验证码的SESSION记录名称，默认为verify
		$length = C('HOME_LENGTH'); //验证码长度
		$useNoise = C('useNoise'); //是关闭验证码杂点
		$type = C('HOME_TYPE'); //验证码字体大
		$width = C('HOME_WIDTH'); //验证码的宽度
		$height = C('HOME_HEIGHT'); //验证码的高度

		$config = array(
			'fontSize' => $type, // 验证码字体大小
			'length' => $length, // 验证码位数
			// 'imageW' => C('HOME_WIDTH'), //0为自动计算
			// 'imageH' => C('HOME_HEIGHT'), //0为自动计算
			'expire' => 10,
			'useNoise' => $useNoise,
		);
		$Verify = new \Think\Verify($config);
		$Verify->entry(1);

	}
	//ajax验证用户名是否正确
	public function AjaxUserName() {

		$this->ajaxReturn(D('User')->Login(), 'JSON');
	}
	//退出登陆清除session
	public function dologinut() {
		//session('[destroy]');
		$_SESSION = array(); //清除客户端session
		//清除cookie
		if (isset($_COOKIE[session_name()])) {
			setcookie(session_name(), '', time() - 1, '/');
		}
		session_destroy(); //清除服务器下session文件
		$this->success('成功退出!', U('Admin/Login/index'));
	}
	//附件上传
	public function Upload() {
		$ROOT = trim(C('rootPath'));
		$config = array(
			'maxSize' => trim(C('maxSize')),
			'rootPath' => $ROOT,
			'saveName' => array('uniqid', ''),
			'exts' => trim(C('exts')),
			'autoSub' => trim(C('autoSub')),
			'subName' => array('date', 'Ymd'),
		);
		$upload = new \Think\Upload($config); // 实例化上传类
		$info = $upload->upload();
		if (!$info) {
			// 上传错误提示错误信息
			$data['info'] = $upload->getError();
			$data['status'] = false;
		} else {
			// 上传成功 获取上传文件信息
			foreach ($info as $file) {
				//默认启用图像
				$path[] = $ROOT . $file['savepath'] . $file['savename'];
			}
			$data['info'] = '上传成功!';
			$data['status'] = true;
			$data['path'] = $path;
		}
		$this->ajaxReturn($data);
	}
	//获取大写价格
	public function getPrice() {
		$price = I('post.price', '', 'intval');
		if ($price) {
			$this->success(cny($price));
		} else {
			$this->error('null');
		}

	}
	//获取授权用户信息
	public function Auth_Action() {
		$p = I('get.p');
		$map['shouquandizhi'] = $p;
		//$map['shouquankey']=I('get.k');
		if ($p) {
			$infos = M('shouquanguanli')->where($map)->getField('daoqishijian');
		}
		if ($infos) {
			$data['status'] = true;
			$data['info'] = '获取成功!';
			$data['data'] = $infos;
		} else {
			$data['status'] = false;
			$data['info'] = '您还未授权!';
		}
		$this->ajaxReturn($data);
	}
}